package net.sk.china.city.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.sk.china.common.exception.ServiceException;
import net.sk.china.city.utils.PlatformIdHolder;
import net.sk.china.city.service.IArExchangeLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.sk.china.city.mapper.ArExchangeLocationMapper;
import net.sk.china.city.domain.ArExchangeLocation;

/**
 * 兑换地点Service业务层处理
 * 
 * @author gxf
 * @date 2024-04-25
 */
@Service
public class ArExchangeLocationServiceImpl implements IArExchangeLocationService
{
    @Autowired
    private ArExchangeLocationMapper arExchangeLocationMapper;

    /**
     * 查询兑换地点
     * 
     * @param id 兑换地点主键
     * @return 兑换地点
     */
    @Override
    public ArExchangeLocation selectArExchangeLocationById(Long id)
    {
        return arExchangeLocationMapper.selectArExchangeLocationById(id);
    }

    /**
     * 查询兑换地点列表
     * 
     * @param arExchangeLocation 兑换地点
     * @return 兑换地点
     */
    @Override
    public List<ArExchangeLocation> selectArExchangeLocationList(ArExchangeLocation arExchangeLocation)
    {
        arExchangeLocation.setPlatformId(PlatformIdHolder.getPlatformId());
        return arExchangeLocationMapper.selectArExchangeLocationList(arExchangeLocation);
    }

    /**
     * 新增兑换地点
     * 
     * @param arExchangeLocation 兑换地点
     * @return 结果
     */
    @Override
    public int insertArExchangeLocation(ArExchangeLocation arExchangeLocation)
    {
        ArExchangeLocation exchangeLocation = arExchangeLocationMapper.selectOne(new QueryWrapper<ArExchangeLocation>()
                .eq("exchange_location", arExchangeLocation.getExchangeLocation())
                .like("platform_id","%" + PlatformIdHolder.getPlatformId() +"%"));
        if(exchangeLocation != null){
            throw new ServiceException("该兑换地点已存在");
        }
        arExchangeLocation.setPlatformId(PlatformIdHolder.getPlatformId());
        return arExchangeLocationMapper.insertArExchangeLocation(arExchangeLocation);
    }

    /**
     * 修改兑换地点
     * 
     * @param arExchangeLocation 兑换地点
     * @return 结果
     */
    @Override
    public int updateArExchangeLocation(ArExchangeLocation arExchangeLocation) {
        ArExchangeLocation exchangeLocation = arExchangeLocationMapper.selectOne(new QueryWrapper<ArExchangeLocation>()
                .eq("id", arExchangeLocation.getId()));

        if (exchangeLocation != null && arExchangeLocation.getExchangeLocation() != null
                && !exchangeLocation.getExchangeLocation().equals(arExchangeLocation.getExchangeLocation())) {
            // 检查是否存在其他兑换地点和当前要更新的地点相同
            ArExchangeLocation checkLocation = arExchangeLocationMapper.selectOne(new QueryWrapper<ArExchangeLocation>()
                    .ne("id", arExchangeLocation.getId()) // 排除当前要更新的地点
                    .eq("exchange_location", arExchangeLocation.getExchangeLocation())
                    .like("platform_id","%" + PlatformIdHolder.getPlatformId() +"%"));

            if (checkLocation != null) {
                throw new ServiceException("该兑换地点已存在");
            }
        }

        return arExchangeLocationMapper.updateArExchangeLocation(arExchangeLocation);
    }

    /**
     * 批量删除兑换地点
     * 
     * @param ids 需要删除的兑换地点主键
     * @return 结果
     */
    @Override
    public int deleteArExchangeLocationByIds(Long[] ids)
    {
        return arExchangeLocationMapper.deleteArExchangeLocationByIds(ids);
    }

    /**
     * 删除兑换地点信息
     * 
     * @param id 兑换地点主键
     * @return 结果
     */
    @Override
    public int deleteArExchangeLocationById(Long id)
    {
        return arExchangeLocationMapper.deleteArExchangeLocationById(id);
    }

    /**
     * 获取所有的兑换地点和id
     * @return
     */
    @Override
    public List<ArExchangeLocation> getLocationAndId(){
        return arExchangeLocationMapper.selectList(new QueryWrapper<ArExchangeLocation>()
                .like("platform_id","%" + PlatformIdHolder.getPlatformId() +"%"));
     }
}
